/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package escr.clases;

import escr.persistencia.Especialidad;
import escr.persistencia.ProEspe;
import escr.persistencia.Profesional;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
 *
 * @author Aditardo
 */
public class Captura {
    
    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("WebAdtafa_EcPU");
    
    public Captura(){
        
    }
    public Profesional getEspecialista(Integer ID) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Object u=null;
        try {
            u = em.createQuery("SELECT p FROM Profesional p WHERE p.proCodigo = :proCodigo").setParameter("proCodigo", ID).getSingleResult();
            if (u==null) {
                return null;
            }
            return (Profesional) u;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;

   }
    
    
    
    public Especialidad getEspecialidad(Integer buscar) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Object obj = null;
        try {
           
            obj = em.createQuery("SELECT e FROM Especialidad e WHERE e.espCod = :espCod").setParameter("espCod", buscar).getSingleResult();
          if(obj == null){
              return null;
          }else
            return (Especialidad) obj;
            
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;
    }
    
        public Integer datTurnN(Date fecha, ProEspe cod)  {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Object u=null;
        try {
            // TODO:
            u = em.createQuery("SELECT max(e.turN) FROM Turno e where e.turCita =:fecha and e.preCod =:cod").setParameter("fecha",fecha).setParameter("cod", cod).getSingleResult();
            if (u==null) {
                return 0;
            }
            return (Integer) u;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;
    }    
        
        public ProEspe getProEspe(Integer buscar) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Object obj = null;
        try {
           
            obj = em.createQuery("SELECT e FROM Especialidad e WHERE e.espCod = :espCod").setParameter("espCod", buscar).getSingleResult();
          if(obj == null){
              return null;
          }else
            return (ProEspe) obj;
            
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;
    }
        
        public List ListarProfesinales(Especialidad buscado) {
        
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        List l=null;
        try {
            // TODO:
         l = em.createQuery("SELECT e FROM Profesional e where e.proCodigo IN (SELECT u.proCodigo.proCodigo FROM ProEspe u where u.espCod =:buscado)").setParameter("buscado", buscado).getResultList();
            System.out.println(""+l);
            return l;
            
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;
    }
        
       public ProEspe GetCodPreCod(Especialidad espe, Profesional pro){
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Object obj = null;
        try {
            // TODO:
             obj = em.createQuery("SELECT e FROM ProEspe e WHERE e.espCod = :espCod and e.proCodigo =:proCodigo").setParameter("espCod", espe).setParameter("proCodigo", pro).getSingleResult();
           
            return (ProEspe)obj;
            
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;
           
           
           
       }
        

    
}


